package se.kth.seds.mi.ui;

import se.kth.seds.mi.communication.Client;
import se.kth.seds.mi.communication.MessageWithMac;
import se.kth.seds.mi.core.common.HashAlgorithm;
import se.kth.seds.mi.core.crypto.sharedsecret.SharedSecretCrypto;
import se.kth.seds.mi.core.exceptions.OperationFailedException;

import javax.swing.*;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: Sike Huang
 * Date: Nov 21, 2008
 * Time: 3:48:31 PM
 * To change this template use File | Settings | File Templates.
 */
public class SharedSecretSenderFrame extends JFrame {
    private String sharedSecret;
    private Client client;
    private SharedSecretCrypto sharedSecretCrypto;

    public void setClient(Client client) {
        this.client = client;
    }

    public void setSharedSecret(String sharedSecret) {
        this.sharedSecret = sharedSecret;
    }

    public void setSharedSecretCrypto(SharedSecretCrypto sharedSecretCrypto) {
        this.sharedSecretCrypto = sharedSecretCrypto;
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    public void initComponents() {

        jLabel_heading = new javax.swing.JLabel();
        jLabel_message = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea_message = new javax.swing.JTextArea();
        jTextField_sharedSecret = new javax.swing.JTextField();
        jLabel_sharedSecret = new javax.swing.JLabel();
        jComboBox_hashFunc = new javax.swing.JComboBox();
        jLabel_hashFunc = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea_generatedMac = new javax.swing.JTextArea();
        jLabel_generatedMac = new javax.swing.JLabel();
        jButton_hashAndSend = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jLabel_heading.setText("Shared Secret Sender"); // NOI18N
        jLabel_heading.setName("jLabel_heading"); // NOI18N

        jLabel_message.setText("Message:"); // NOI18N
        jLabel_message.setName("jLabel_message"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextArea_message.setColumns(20);
        jTextArea_message.setRows(5);
        jTextArea_message.setName("jTextArea_message"); // NOI18N
        jTextArea_message.setLineWrap(true);
        jScrollPane1.setViewportView(jTextArea_message);

        jTextField_sharedSecret.setText(""); // NOI18N
        jTextField_sharedSecret.setName("jTextField_sharedSecret"); // NOI18N

        jLabel_sharedSecret.setText("Shared Secret:"); // NOI18N
        jLabel_sharedSecret.setName("jLabel_sharedSecret"); // NOI18N

        jComboBox_hashFunc.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Item 1", "Item 2", "Item 3", "Item 4"}));
        jComboBox_hashFunc.setName("jComboBox_hashFunc"); // NOI18N

        jLabel_hashFunc.setText("Hash Func:"); // NOI18N
        jLabel_hashFunc.setName("jLabel_hashFunc"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTextArea_generatedMac.setColumns(20);
        jTextArea_generatedMac.setRows(5);
        jTextArea_generatedMac.setName("jTextArea_generatedMac"); // NOI18N
        jTextArea_generatedMac.setLineWrap(true);
        jTextArea_generatedMac.setEditable(false);
        jScrollPane2.setViewportView(jTextArea_generatedMac);

        jLabel_generatedMac.setText("Generated MAC:"); // NOI18N
        jLabel_generatedMac.setName("jLabel_generatedMac"); // NOI18N

        jButton_hashAndSend.setText("Hash & Send"); // NOI18N
        jButton_hashAndSend.setName("jButton_hashAndSend"); // NOI18N
        jButton_hashAndSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_hashAndSendActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(144, 144, 144)
                                .addComponent(jLabel_heading)
                                .addGap(151, 151, 151))
                        .addGroup(layout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel_sharedSecret)
                                .addComponent(jLabel_message)
                                .addComponent(jLabel_hashFunc)
                                .addComponent(jLabel_generatedMac))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton_hashAndSend)
                                .addContainerGap())
                        .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                                .addComponent(jComboBox_hashFunc, javax.swing.GroupLayout.Alignment.LEADING, 0, 200, Short.MAX_VALUE)
                                .addComponent(jTextField_sharedSecret, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE))
                        .addGap(61, 61, 61))))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel_heading)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel_message))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jTextField_sharedSecret, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel_sharedSecret))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jComboBox_hashFunc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel_hashFunc))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel_generatedMac))
                        .addGap(18, 18, 18)
                        .addComponent(jButton_hashAndSend)
                        .addContainerGap(15, Short.MAX_VALUE))
        );

        jComboBox_hashFunc.setModel(new DefaultComboBoxModel(HashAlgorithm.values()));
        jTextField_sharedSecret.setText(sharedSecret);

        pack();
        setVisible(true);
    }// </editor-fold>

    private void jButton_hashAndSendActionPerformed(java.awt.event.ActionEvent evt) {
        String message = jTextArea_message.getText();
        String sharedSecret = jTextField_sharedSecret.getText();
        HashAlgorithm algorithm = (HashAlgorithm) jComboBox_hashFunc.getSelectedItem();
        if (sharedSecret.trim().equals("")) {
            JOptionPane.showMessageDialog(this, "shard secret is required!", "Validation Error", JOptionPane.ERROR_MESSAGE);
        } else if (message.trim().equals("")) {
            JOptionPane.showMessageDialog(this, "message is required!", "Validation Error", JOptionPane.ERROR_MESSAGE);
        } else {
            // calculate MAC on client side, and send messsage together with MAC to server
            sharedSecretCrypto.setMessage(message);
            sharedSecretCrypto.setSharedSecret(sharedSecret);
            sharedSecretCrypto.setHashAlgorithm(algorithm);
            String mac = null;
            try {
                mac = sharedSecretCrypto.createMAC();
                jTextArea_generatedMac.setText(mac);
            } catch (OperationFailedException e1) {
                e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                JOptionPane.showMessageDialog(this, e1.getMessage(), "Crypto Exception", JOptionPane.ERROR_MESSAGE);
            }
            if (mac != null) {
                MessageWithMac messageWithMac = new MessageWithMac();
                messageWithMac.setMac(mac);
                messageWithMac.setMessage(message);
                try {
                    client.send(messageWithMac);
                } catch (IOException e1) {
                    e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    JOptionPane.showMessageDialog(this, e1.getMessage(), "Network Exception", JOptionPane.ERROR_MESSAGE);
                }
            }
            MessageWithMac messageWithMac = new MessageWithMac();
            messageWithMac.setMac(mac);
            messageWithMac.setMessage(message);
            try {
                client.send(messageWithMac);
            } catch (IOException e1) {
                e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton_hashAndSend;
    private javax.swing.JComboBox jComboBox_hashFunc;
    private javax.swing.JLabel jLabel_generatedMac;
    private javax.swing.JLabel jLabel_hashFunc;
    private javax.swing.JLabel jLabel_heading;
    private javax.swing.JLabel jLabel_message;
    private javax.swing.JLabel jLabel_sharedSecret;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea_generatedMac;
    private javax.swing.JTextArea jTextArea_message;
    private javax.swing.JTextField jTextField_sharedSecret;
    // End of variables declaration
}
